package com.achievement.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.achievement.entity.Course;
import com.achievement.entity.Subject;
import com.achievement.entity.dto.SubjectDTO;
import com.achievement.service.ISubjectService;
import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/subject")
public class SubjectController {
	@Autowired
	private ISubjectService subjectService;
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Subject> findAll(){
		return subjectService.findAll();
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Subject> findPage(JpaPageable pageable,String str){
		try {
			return subjectService.findPage(SubjectDTO.getSpecification(str), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(Subject subject) {
		try {
			if(subjectService.isNumExist(subject))
				return new AjaxResponse(false, "编号已存在!");
			subjectService.saveOrUpdate(subject);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(subjectService.hasTeacher(id))
				return new AjaxResponse(false, "科目存在教师，请先移除！");
			if(subjectService.hasCourse(id))
				return new AjaxResponse(false, "科目存在课程，请先移除!");
			subjectService.delete(id);
			return new AjaxResponse(true, "已删除!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败...");
		}
	}

}
